#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [新大陆集成服务一部-系统组].
# Create Date:
# Update Date:
__authors__ = "新大陆集成服务一部-系统组"

"""4A数据文件导出上传程序.
可以实现两个功能，数据文件导出和ftp上传到服务器.

执行结果说明:

时间戳: 执行时间
数据文件来源IP地址: 识别文件来源
执行结果状态:
    Local file UnComplete - 本地文件还未生成
    Miss local file[26_4Alog_20150205.dmp] - 本地文件缺失


"""

import os
import sys
import datetime
import glob
import ConfigParser
from ftplib import FTP


class ExpPutData():
    def __init__(self):
        self.cf = ConfigParser.ConfigParser()
        conffile = os.path.join(os.path.split(os.path.realpath(__file__))[0], "4a_nldata_exp_put_data.ini")
        self.cf.read(conffile)

        self.step = self.cf.getint("config", "step")
        self.operation = self.cf.getint("config", "operation")
        self.reserve = self.cf.getint("config", "reserve")

        self.expcmd = self.cf.get("exp", "cmd")

        self.localdir = self.cf.get("ftp", "localdir")
        self.iplist = self.cf.get("ftp", "iplist").split(",")
        self.ftphost = self.cf.get("ftp", "ftphost")
        self.ftpport = self.cf.get("ftp", "ftpport")
        self.ftpuser = self.cf.get("ftp", "ftpuser")
        self.ftppasswd = self.cf.get("ftp", "ftppasswd")
        self.targetdir = self.cf.get("ftp", "targetdir")

        self.prestep = self.step - 1
        self.nextstep = self.step + 1

        self.step = str(self.step)
        self.prestep = str(self.prestep)
        self.nextstep = str(self.nextstep)
        self.curcomplete = self.step + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.predaycurcomplete = self.step + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today() - datetime.timedelta(days=1), "%Y%m%d"))
        self.curputting = self.step + "putting" + str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.precomplete = self.prestep + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d"))
        self.predayprecomplete = self.prestep + "complete" + str(
            datetime.datetime.strftime(datetime.datetime.today() - datetime.timedelta(days=1), "%Y%m%d"))

        self.filesuffix = "4AData" + datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")
        self.hisfilesuffix = "4AData" + datetime.datetime.strftime(
            datetime.datetime.now() - datetime.timedelta(days=self.reserve),
            "%Y%m%d")

        self.pid = os.path.join(self.localdir, str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")))
        if int(self.step) > 1 and (not os.path.isfile(os.path.join(self.localdir, self.precomplete))):
            print "Pre step not complete !!!\n The Pre step file: " + os.path.join(self.localdir, self.precomplete)
            sys.exit()
        if os.path.isfile(self.pid):
            print "Have another process operation. Cancel operation!!!\n The pid file: " + self.pid
            sys.exit()
        if os.path.isfile(os.path.join(self.localdir, self.curcomplete)):
            print "Current step tasks for today already done.\n The flag file: " + os.path.join(self.localdir,
                                                                                                self.curcomplete)
            sys.exit()
        # if int(self.step) > 1 and (not os.path.isfile(os.path.join(self.localdir, self.precomplete))):
        # print "Previously step tasks for today not complete.\n Not exists the flag file" + os.path.join(
        # self.localdir, self.precomplete)
        # sys.exit()

        try:
            os.system("touch " + self.pid)
            self.delfile()
            if self.operation == 1:
                self.expdata()
            if self.operation == 2:
                self.ftpputdata()
            if self.operation == 3:
                self.expdata()
                self.ftpputdata()
        except Exception, e:
            print e
            sys.exit()
        finally:
            os.remove(self.pid)

    def expdata(self):
        try:
            for cmd in self.expcmd.split("||"):
                cmd = cmd.replace("$YYYYMMDD$", str(datetime.datetime.strftime(datetime.datetime.today(), "%Y%m%d")))
                os.system("source ~/.bash_profile;" + cmd)
            for ip in self.iplist:
                zfilelist = ""
                if glob.glob(self.localdir + os.path.sep + ip + self.filesuffix + "*.dmp") or glob.glob(
                                                        self.localdir + os.path.sep + ip + self.filesuffix + "*.log"):
                    for dmpfile in (glob.glob(self.localdir + os.path.sep + ip + self.filesuffix + "*.dmp")):
                        zfilelist += dmpfile + " "
                    for dmpfile in (glob.glob(self.localdir + os.path.sep + ip + self.filesuffix + "*.log")):
                        zfilelist += dmpfile + " "
                    cmd = "cd " + self.localdir + ";zip -mj " + ip + self.filesuffix + ".zip " + zfilelist
                    os.system(cmd)
        except Exception, e:
            os.remove(self.pid)
            print e
            sys.exit()

    def ftpputdata(self):
        try:
            print " "
            print "#" * 120
            print "#" + "Exp the 4ALog data, and put to the target FTPSer".center(118) + "#"
            print "#" * 120
            print "+" + "-" * 26 + "+" + "-" * 3 + "+" + "-" * 62 + "+" + "-" * 24 + "+"
            print "|" + "Timestapm".center(26) + "|" + "ip".center(3) + "|" + "Filestatus".center(
                62) + "|" + "PutStatus".center(24) + "|"
            print "+" + "-" * 26 + "+" + "-" * 3 + "+" + "-" * 62 + "+" + "-" * 24 + "+"
            ipnum = len(self.iplist)
            sucnum = 0
            for ip in self.iplist:
                print "|" + str(datetime.datetime.now()).center(26) + "|" + ip.center(3) + "|",

                localzipfile = self.localdir + os.path.sep + ip + self.filesuffix + ".zip"
                presetpcomplete = self.localdir + os.path.sep + self.precomplete + ip
                if (int(self.step) == 1 and os.path.isfile(localzipfile)) or (
                                    int(self.step) > 1 and os.path.isfile(localzipfile) and os.path.isfile(
                            presetpcomplete)):
                    put = self.ftpup(localzipfile, self.targetdir, os.path.basename(localzipfile), str(ip))
                    print localzipfile.center(61) + "+" + put.center(24) + "+"
                    sucnum += 1
                else:
                    print ("Miss local file[" + localzipfile + "]").center(61) + "+" + " " * 24 + "+"
            if ipnum == sucnum:
                os.system("touch " + os.path.join(self.localdir, self.curcomplete))
                self.ftpup(self.pid, self.targetdir, self.curcomplete, 'ALL')
        except Exception, e:
            os.remove(self.pid)
            print e
            sys.exit()
        finally:
            print "+" + "-" * 26 + "+" + "-" * 3 + "+" + "-" * 62 + "+" + "-" * 24 + "+"
            print "#" + "Finally".center(118) + "#"
            print "#" * 120

    def ftpup(self, sourcefile, targetdir, targetfile, checkflag):
        ftpser = FTP()
        ftpser.connect(self.ftphost, self.ftpport)
        ftpser.login(user=self.ftpuser, passwd=self.ftppasswd)
        ftpser.set_pasv(False)
        try:
            filelist = ftpser.nlst(targetdir)
            blankfile = self.pid
            operationcomplete = self.curcomplete + checkflag
            operationputting = self.curputting + checkflag
            filenamelist = map(os.path.basename, filelist)
            if operationcomplete in filenamelist:
                return "File Already Exists"
            elif operationputting in filenamelist:
                return "File Putting"
            else:
                if checkflag == 'ALL':
                    ftpser.storbinary("STOR " + targetdir + "/" + targetfile, open(blankfile, "rb"))
                else:
                    ftpser.storbinary("STOR " + targetdir + "/" + operationputting, open(blankfile, "rb"))
                    ftpser.storbinary("STOR " + targetdir + "/" + targetfile, open(sourcefile, "rb"))
                    ftpser.delete(targetdir + "/" + operationputting)
                    ftpser.storbinary("STOR " + targetdir + "/" + operationcomplete, open(blankfile, "rb"))
                    return "Put Done"
        except Exception, e:
            os.remove(self.pid)
            print e
            sys.exit()
        finally:
            ftpser.quit()

    def delfile(self):
        for rmfile in glob.glob(os.path.join(self.localdir, self.predaycurcomplete)):
            os.remove(rmfile)
        for ip in self.iplist:
            for rmfile in glob.glob(os.path.join(self.localdir, ip + self.hisfilesuffix + "*")):
                os.remove(rmfile)
            for rmfile in glob.glob(os.path.join(self.localdir, self.predayprecomplete + "*")):
                os.remove(rmfile)
                # for rmfile in glob.glob(os.path.join(self.localdir, ip + self.hisfilesuffix)):
                # os.remove(rmfile)
                # for rmfile in glob.glob(os.path.join(self.localdir, ip + self.hisfilesuffix)):
                # os.remove(rmfile)


if __name__ == "__main__":
    do = ExpPutData()
